package com.oamicnet.mapstruct.service.gateway.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class LogRequestGlobalFilter implements GlobalFilter, Ordered {

    private static final Logger logger = LoggerFactory.getLogger(LogRequestGlobalFilter.class);

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 打印请求信息
        logger.info("请求路径: {}", exchange.getRequest().getPath());
        logger.info("请求方法: {}", exchange.getRequest().getMethod());
        logger.info("请求IP: {}", exchange.getRequest().getRemoteAddress());
        
        return chain.filter(exchange)
                .then(Mono.fromRunnable(() -> {
                    // 打印响应状态码
                    logger.info("响应状态码: {}", exchange.getResponse().getStatusCode());
                }));
    }

    @Override
    public int getOrder() {
        return -1; // 优先级，越小越靠前
    }
}
